package com.Blog.www.servlet;

import com.Blog.www.constant.DataConstant;
import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.po.Column;
import com.Blog.www.po.PageBean;
import com.Blog.www.service.ColumnService;
import com.Blog.www.service.Impl.ColumnServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author 君未洋
 */
@WebServlet("/Column/*")
public class ColumnServlet extends BaseServlet {

    private static ColumnService service = new ColumnServiceImpl();

    public void deleteColumn(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //获取下要修改的文章专栏
        String[] columnIds = req.getParameterValues("columnId");
        if (columnIds == null || columnIds.length == 0) {
            req.getRequestDispatcher("/Column/deleteColumnList").forward(req, resp);
        }

        req.removeAttribute(PageConstant.COLUMN_A);
        //没问题直接去操作了
        if (service.deleteList(columnIds)) {
            req.setAttribute(PageConstant.COLUMN_A, TipsConstant.BLOG_OPERATION_B);
        } else {
            req.setAttribute(PageConstant.COLUMN_A, TipsConstant.BLOG_OPERATION_A);
        }

        req.getRequestDispatcher("/Column/deleteColumnList").forward(req, resp);
    }

    /**
     * 展示文章的专栏用
     */
    public void deleteColumnList(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //直接获取后展示
        //获取下现在专栏的地址
        String rows = req.getParameter("rows");
        String currentPage = req.getParameter("currentPage");
        String prior = req.getParameter("columnId");

        PageBean<Column> columnByPage = service.findColumnByPage(rows, currentPage, prior);


        req.removeAttribute(DataConstant.COLUMN_A);
        req.setAttribute(DataConstant.COLUMN_A, columnByPage);
        //跳转到准备好的操作界面操作
        req.getRequestDispatcher("/columnList.jsp").forward(req, resp);
        return;
    }

    /**
     * 修改文章的专栏用
     */
    public void addColumn(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //获取下添加专栏需要的数据
        String Id = req.getParameter("addColumnId");
        String bid = req.getParameter("id");
        String uid = req.getParameter("uid");
        String addColumn = req.getParameter("addColumn");
        String prior = req.getParameter("prior");

        req.removeAttribute(PageConstant.BLOG_G);
        //调用方法添加咯
        if (service.addColumn(Id, bid, addColumn, prior)) {
            req.setAttribute(PageConstant.BLOG_G, TipsConstant.BLOG_COLUMN_A);
        } else {
            req.setAttribute(PageConstant.BLOG_G, TipsConstant.BLOG_COLUMN_B);
        }

        req.getRequestDispatcher("/UserBlog/userBlog?uid=" + uid + "&id=" + bid).forward(req, resp);
    }

    public void columnPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //专栏相关内容的获取
        String crows = req.getParameter("crows");
        String ccurrentPage = req.getParameter("ccurrentPage");
        String columnId = req.getParameter("columnId");

        PageBean<Column> columnPageBean = service.findColumnByPage(crows, ccurrentPage, columnId);
        req.setAttribute(DataConstant.COLUMN_A, columnPageBean);
        req.setAttribute(DataConstant.COLUMN_B, columnId);
        //跳转回博客添加页面

        req.getRequestDispatcher("/addBlog.jsp").forward(req, resp);
    }

}
